import { ecs } from "../../../../core/ecs/ECS";
import { RenderComponent } from "../componet/renderComponet";

/**
 * 玩家实体
 * 
 * 说明：
 * 1. 必须实现 init() 方法，否则会报错
 * 2. init() 方法在每次从对象池获取实体时都会调用
 * 3. 在 init() 中添加默认组件和初始化状态
 * 
 * @example
 * // 创建玩家实体
 * const player = ecs.getEntity(Player);
 * player.setPosition(100, 200);
 * 
 * // 销毁实体（会回收到对象池）
 * player.destroy();
 */
@ecs.register('Player')
export class Player extends ecs.Entity {
    // 组件类型声明（方便 IDE 自动补全和类型检查）
    RenderComponent!: RenderComponent;
    
    // 玩家特有属性
    public maxHealth: number = 100;
    public currentHealth: number = 100;
    public level: number = 1;
    public score: number = 0;
    
    /**
     * 初始化方法 - 必须实现！
     * 
     * 重要：
     * - 每次从对象池获取实体时都会调用此方法
     * - 必须在这里添加实体的默认组件
     * - 必须在这里重置所有状态，避免对象池复用时的状态残留
     */
    init(): void {
        // 添加默认组件
        this.add(RenderComponent);
        
        // 重置组件初始值
        this.RenderComponent.visible = true;
        this.RenderComponent.alpha = 1.0;
        this.RenderComponent.zIndex = 0;
        
        // 重置玩家状态（重要！避免对象池复用时的状态残留）
        this.maxHealth = 100;
        this.currentHealth = 100;
        this.level = 1;
        this.score = 0;
    }
    
    /**
     * 设置位置（示例方法）
     */
    setPosition(x: number, y: number): this {
        // TODO: 当添加了 PositionComponent 后可以使用
        // this.Position.set(x, y);
        console.log(`Player position set to: ${x}, ${y}`);
        return this;
    }
    
    /**
     * 受到伤害
     */
    takeDamage(damage: number): void {
        this.currentHealth = Math.max(0, this.currentHealth - damage);
        
        if (this.currentHealth === 0) {
            this.onDeath();
        } else {
            console.log(`Player took ${damage} damage. HP: ${this.currentHealth}/${this.maxHealth}`);
        }
    }
    
    /**
     * 治疗
     */
    heal(amount: number): void {
        const oldHealth = this.currentHealth;
        this.currentHealth = Math.min(this.maxHealth, this.currentHealth + amount);
        const actualHeal = this.currentHealth - oldHealth;
        
        if (actualHeal > 0) {
            console.log(`Player healed ${actualHeal} HP. HP: ${this.currentHealth}/${this.maxHealth}`);
        }
    }
    
    /**
     * 升级
     */
    levelUp(): void {
        this.level++;
        this.maxHealth += 10;
        this.currentHealth = this.maxHealth;
        console.log(`Player leveled up to ${this.level}! Max HP increased to ${this.maxHealth}`);
    }
    
    /**
     * 增加分数
     */
    addScore(points: number): void {
        this.score += points;
        console.log(`Score: ${this.score} (+${points})`);
    }
    
    /**
     * 死亡逻辑
     */
    private onDeath(): void {
        console.log('Player died!');
        this.RenderComponent.visible = false;
        
        // TODO: 触发死亡动画、游戏结束等逻辑
    }
    
    /**
     * 检查是否存活
     */
    isAlive(): boolean {
        return this.currentHealth > 0;
    }
    
    /**
     * 获取生命值百分比
     */
    getHealthPercentage(): number {
        return (this.currentHealth / this.maxHealth) * 100;
    }
}
